package com.ljf.interestingmaps.streetview;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.ljf.interestingmaps.R;
import com.ljf.interestingmaps.adapter.OnRecyclerViewItemClickListener;
import com.ljf.interestingmaps.adapter.PlaceAdapter;
import com.ljf.interestingmaps.util.NetUtils;
import com.ljf.interestingmaps.view.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class StreetViewActivity extends AppCompatActivity {

    private List<StreetViewBean> bigPlaceList = new ArrayList<>();
    private RecyclerView recyclerView;
    private PlaceAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.streetview_layout);

        initView();
        loadDataAndInit();
    }

    private void initView() {
        recyclerView = findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PlaceAdapter(this);
        recyclerView.setAdapter(adapter);

        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(StreetViewActivity.this, MapandStreetView.class);
                intent.putExtra("StreetBean", adapter.getItems().get(position));
                startActivity(intent);
            }
        });
    }

    void loadDataAndInit(){
        LoadingDialog.show(this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                final String data = NetUtils.getUrlContent("https://www.google.com/streetview/feed/gallery/data.json");

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject jsonObject = new JSONObject(data);
                            Iterator<String> iterator = jsonObject.keys();

                            while (iterator.hasNext()){
                                String key = iterator.next();
                                JSONObject jsonObject1 = jsonObject.getJSONObject(key);
                                StreetViewBean bigPlace = new StreetViewBean();
                                bigPlace.setStreetName(jsonObject1.getString("title"));
                                bigPlace.setLat(jsonObject1.getDouble("lat"));
                                bigPlace.setLang(jsonObject1.getDouble("lng"));
                                bigPlace.setKey(key);
                                bigPlace.setPanoid(jsonObject1.getString("panoid"));
                                if("LiAWseC5n46JieDt9Dkevw".equals(bigPlace.getPanoid())){      //这个没图片
                                    continue;
                                }
                                if(jsonObject1.has("isFife")){
                                    bigPlace.setFife(jsonObject1.getBoolean("isFife"));
                                    continue;   //这种情况略过
                                }

                                if(bigPlace.getFife()){
                                    bigPlace.setPic_Url("https://lh4.googleusercontent.com/"+bigPlace.getPanoid()+"/w400-h300-fo90-ya0-pi0/");
                                    continue;   //这种情况略过


                                }else {
                                    bigPlace.setPic_Url("https://geo0.ggpht.com/cbk?output=thumbnail&thumb=2&panoid="+jsonObject1.getString("panoid"));

                                }
                                bigPlaceList.add(bigPlace);
                            }
                            adapter.getItems().addAll(bigPlaceList);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }finally {
                            LoadingDialog.dissmiss(StreetViewActivity.this);
                        }
                    }
                });

            }
        }).start();
    }
}
